#include "openmove_Prefix.h"
#include "LightEditor.h"

LightEditor :: LightEditor(CEGUI::Window *parent, SceneManager *sceneManager) : m_light(NULL), m_sceneManager(sceneManager)
{
	CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
	m_window = wmgr.createWindow((CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)"lightEditor");
	m_window->setVisible(false);
	m_window->setPosition(CEGUI::Point(0, 0.05f));
	m_window->setSize(CEGUI::Size(0.16f, 0.7f));
	parent->addChildWindow(m_window);

	CEGUI::Size size(1.0f, 0.16f);

	m_diffuse = new RGBASlider("lightDiffuse", m_window, Vector4::ZERO, CEGUI::Point(0, 0), size);
	m_ambient = new RGBASlider("lightAmbient", m_window, Vector4::ZERO, CEGUI::Point(0, 0.16), size);
	m_specular = new RGBASlider("lightSpecular", m_window, Vector4::ZERO, CEGUI::Point(0, 0.32), size);
	m_position = new RGBASlider("lightPosition", m_window, Vector4::ZERO, CEGUI::Point(0, 0.48), size);
	m_direction = new RGBASlider("lightDirection", m_window, Vector4::ZERO, CEGUI::Point(0, 0.64), size);
	m_diffuse->setMin(0.0f); m_diffuse->setMax(1.0f);
	m_ambient->setMin(0.0f); m_ambient->setMax(1.0f);
	m_specular->setMin(0.0f); m_specular->setMax(1.0f);
	m_position->setMin(-1000.0f); m_position->setMax(1000.0f);
	m_direction->setMin(-5.0f); m_direction->setMax(5.0f);

	m_ok = (CEGUI::PushButton*)wmgr.createWindow("TaharezLook/Button", (CEGUI::utf8*)"lightOk");
	m_window->addChildWindow(m_ok);
	m_ok->setPosition(CEGUI::Point(0.f, 0.80f));
	m_ok->setSize(size);
	m_ok->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&LightEditor::handleOkEvent, this));
	m_ok->setText("Ok");
}

LightEditor :: ~LightEditor(void)
{
	delete m_diffuse;
	delete m_ambient;
	delete m_specular;
	delete m_position;
	delete m_direction;
}

bool LightEditor :: handleOkEvent(const CEGUI::EventArgs& e)
{
	m_window->getParent()->activate();
	m_window->setVisible(false);

	return (true);
}

bool LightEditor :: isActive(void)
{
	return (m_window->isVisible() && m_window->isActive());
}

bool LightEditor :: isVisible(void)
{
	return (m_window->isVisible());
}

void LightEditor :: setVisible(bool visible)
{
	m_window->setVisible(visible);
}

void LightEditor :: updateSliders(Light const *light)
{
	m_diffuse->updateSlider(light->getDiffuseColour());
	m_specular->updateSlider(light->getSpecularColour());
	m_ambient->updateSlider(m_sceneManager->getAmbientLight());
	m_position->updateSlider(light->getPosition());
	m_direction->updateSlider(light->getDirection());
}

void LightEditor :: updateLight(Light *light)
{
	light->setDiffuseColour(m_diffuse->getValueColour());
	light->setSpecularColour(m_specular->getValueColour());
	m_sceneManager->setAmbientLight(m_ambient->getValueColour());
	light->setPosition(m_position->getValueVector3());
	light->setDirection(m_direction->getValueVector3());
}

void LightEditor :: attach(CEGUI::Window *parent)
{
	m_window->removeChildWindow(m_window->getParent());
	parent->addChildWindow(m_window);
}
